// 系统日志工具函数

import { RequestMethod, SysLog } from '../types';

// 格式化请求方法显示文本
export const formatRequestMethod = (method: RequestMethod): string => {
  const methodMap: Record<RequestMethod, string> = {
    [RequestMethod.GET]: 'GET',
    [RequestMethod.POST]: 'POST',
    [RequestMethod.PUT]: 'PUT',
    [RequestMethod.DELETE]: 'DELETE',
    [RequestMethod.PATCH]: 'PATCH',
    [RequestMethod.OPTIONS]: 'OPTIONS',
    [RequestMethod.HEAD]: 'HEAD',
  };
  return methodMap[method] || method;
};

// 格式化响应状态码颜色
export const getResponseCodeColor = (code: number | null): string => {
  if (code === null) return 'error';
  if (code >= 200 && code < 300) return 'success';
  if (code >= 300 && code < 400) return 'warning';
  if (code >= 400) return 'error';
  return 'default';
};

// 格式化耗时显示
export const formatCostTime = (costTime: number): string => {
  if (costTime < 1000) {
    return `${costTime}ms`;
  } else if (costTime < 60000) {
    return `${(costTime / 1000).toFixed(2)}s`;
  } else {
    const minutes = Math.floor(costTime / 60000);
    const seconds = Math.floor((costTime % 60000) / 1000);
    return `${minutes}m ${seconds}s`;
  }
};

// 格式化日期时间
export const formatDateTime = (dateTime: string): string => {
  const date = new Date(dateTime);
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
  });
};

// 判断是否为错误请求
export const isErrorRequest = (log: SysLog): boolean => {
  return log.responseCode === null || log.responseCode >= 400;
};

// 格式化JSON字符串显示
export const formatJsonString = (jsonStr: string): string => {
  try {
    const parsed = JSON.parse(jsonStr);
    return JSON.stringify(parsed, null, 2);
  } catch {
    return jsonStr;
  }
};

// 截断长文本
export const truncateText = (text: string, maxLength: number = 100): string => {
  if (text.length <= maxLength) return text;
  return text.substring(0, maxLength) + '...';
};

// 从请求参数中提取文件信息
export const extractFileInfo = (requestParams: string): string[] => {
  const filePattern = /文件:\s*([^(]+)\s*\((\d+)\s*bytes\)/g;
  const files: string[] = [];
  let match;
  
  while ((match = filePattern.exec(requestParams)) !== null) {
    const fileName = match[1].trim();
    const fileSize = parseInt(match[2]);
    const sizeStr = formatLogFileSize(fileSize);
    files.push(`${fileName} (${sizeStr})`);
  }
  
  return files;
};

// 格式化文件大小（日志专用）
export const formatLogFileSize = (bytes: number): string => {
  if (bytes === 0) return '0 B';
  const k = 1024;
  const sizes = ['B', 'KB', 'MB', 'GB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
};

// 获取操作类型的颜色标签
export const getOperationColor = (operation: string): string => {
  if (operation.includes('创建') || operation.includes('新增')) return 'success';
  if (operation.includes('删除') || operation.includes('移除')) return 'error';
  if (operation.includes('更新') || operation.includes('修改')) return 'warning';
  if (operation.includes('查询') || operation.includes('获取')) return 'info';
  return 'default';
}; 